﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
using AdaptiveHelpDesk;

namespace Novice
{
    [WebService(Namespace = "http://soaiui.nmmu.ac.za/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)] 
    
    //invoked by the textchange method of the search textbox
    public class SearchResults : System.Web.Services.WebService
    {
        //Hashtable and arraylist used to load search results
        Hashtable ht = new Hashtable();
        ArrayList values = new ArrayList();
        

        //initialise stored procedures
        StoredProcedures sp = new StoredProcedures();

        [WebMethod]
        public List<String> searchResults_old(String searchBy, String SearchValue)
        {
            List<String> names = new List<String>();
            ht = sp.getCustomer(searchBy, SearchValue);

            if (ht.Count != 0)
            {
                foreach (DictionaryEntry de in ht)
                {
                    values = (ArrayList)de.Value;
                    names.Add(values[0].ToString());
                }                
            }
            return names;
        }

        [WebMethod]
        public SearchResult[] searchResults(String searchBy, String SearchValue)
        {
            SearchResult[] names;
            int count = 0;
            ht = sp.getCustomer(searchBy, SearchValue);

            if (ht.Count != 0)
            {
                names = new SearchResult[ht.Count];

                foreach (DictionaryEntry de in ht)
                {
                    values = (ArrayList)de.Value;
                    names[count] = (new SearchResult(values[0].ToString(), values[1].ToString(), de.Key.ToString()));
                    count++;
                }
                return names;
            }
            names = new SearchResult[1];
            names[0] = new SearchResult("", "Customer not found", "");

            return names; //empty list
        }
    }

    public class SearchResult
    {
        public SearchResult(String fn, String sn, String id)
        {
            this.FirstName = fn;
            this.Surname = sn;
            this.UserID = id;
        }

        public SearchResult() { }

          public String FirstName { get; set; }
          public String Surname { get; set; }
          public String UserID { get; set; }
    }
}
